import json
from models import BaseModels
from sqlalchemy import Column, BigInteger, DateTime, String, JSON, Text


class Logs(BaseModels):
    __tablename__ = 'logs'
    id = Column(BigInteger, autoincrement=True, primary_key=True, unique=True, nullable=False)
    route = Column(String(64), nullable=False, comment="请求路径")
    method = Column(String(64), nullable=False, comment="请求方式")
    name = Column(String(64), nullable=True, comment="操作名称")
    user = Column(String(64), nullable=True, comment="操作者名字")
    # obsolescence = Column(JSON, nullable=True, comment="老旧参数")
    # data = Column(JSON, nullable=True, comment="请求操作参数")

    obsolescence = Column(Text, nullable=True, comment="老旧参数")
    data = Column(Text, nullable=True, comment="请求操作参数")

    time = Column(DateTime, nullable=False, index=True, comment="记录创建时间")

    def __repr__(self):
        return json.dumps({
            "log_id": self.id,
            "route": self.route,
            "method": self.method,
            "name": self.name,
            "user": self.user,
            "data": self.data,
            "obsolescence": self.obsolescence,
            "time": self.time.strftime("%Y-%m-%d %H:%M:%S")
        })
